package com.ruoyi.dataserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.dataserver.model.line.LINE;
import com.ruoyi.dataserver.service.ISearchTableService;
import com.ruoyi.dataserver.service.IUpSteamAnalyzeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName
 * @Package com.lab.dataserver.service.impl
 * @ClassName TopologyAnalyzeServceImpl
 * @Description 拓扑分析逻辑层
 * @Company lab
 * @Author wangw
 * @Date 2020/7/9 0009 11:55
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Slf4j
@Component
public class UpStreamAnalyzeServceImpl implements IUpSteamAnalyzeService {

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Autowired
    public ISearchTableService tableService;

/*    @Override
    public Map upStreamAnalyze(String dm, String usidParm) {
        List<String> us_idList = new ArrayList();
        List<String> nodeIDList = new ArrayList();
        String tableNameString = tableService.searchLineTable(dm);
        //----------------------------获取USID-------------------------------
        String sqlWhere = "select DM,QDDH,ZDDH,QDGC,ZDGC,QDMS,ZDMS,MSLX,CZ,GJ,DYZ,YL,LX,ZKS,ZYKS,DLTS,BHCZ,XX,GXFLDM,SZDL,QSDW,JSND,TCDW,TCRQ,SYZT,BZ,SNODID,ENODID,RKDATE,ISLOCK,US_ID from "+tableNameString+" where US_ID='" + usidParm + "'";
        //                        1   2    3     4    5    6    7   8    9  10 11  12 13 14  15   16    17  18  19    20    21  22    23   24   25  26  27     28     29     30     31
        System.out.println(sqlWhere);
        List<Map<String, Object>> usidCursors = ToQueryFeatureCursor(sqlWhere);
        us_idList.add(usidParm);
        for(Map<String, Object> usidCursor :usidCursors){
            System.out.println(JSONObject.toJSONString(usidCursor));
            nodeIDList.add(ToQueryID(usidCursor));
        }
        while (nodeIDList.size() != 0)
        {
            String[] temp = nodeIDList.toArray(new String[nodeIDList.size()]);
            nodeIDList.clear();
            for (String item : temp)
            {
                String usidQuery = "select LX,SNODID,ENODID,US_ID,QSDW,TCDW,SZDL,QDDH,ZDDH,GJ,CZ,QDGC,ZDGC,QDMS,ZDMS from "+tableNameString+" where SNODID='" + item + "'";//起点到终点方向，找到下一条管线
                List<Map<String, Object>> tempCursors = ToQueryFeatureCursor(usidQuery);
                for(Map<String, Object> tempCursor: tempCursors){

                String usid1 = tempCursor.get("US_ID").toString();
                String tempLx1 = tempCursor.get("LX").toString();
                if (!us_idList.contains(usid1) && ("1".equals(tempLx1)||"".equals(tempLx1)||"null".equals(tempLx1)))//&&us_idList.size()<=100
                {
                    us_idList.add(usid1);
                    nodeIDList.add(tempCursor.get("ENODID").toString());
                }
            }
                String usid_1 = "select LX,SNODID,ENODID,US_ID,QSDW,TCDW,SZDL,QDDH,ZDDH,GJ,CZ,QDGC,ZDGC,QDMS,ZDMS from "+tableNameString+" where ENODID='" + item + "'";//起点到终点方向，找到下一条管线

                List<Map<String, Object>> tempCursor1 = ToQueryFeatureCursor(usid_1);
                for(Map<String, Object> tempCursor:tempCursor1){

                    String usid1 = tempCursor.get("US_ID").toString();
                    String tempLx1 = tempCursor.get("LX").toString();
                    if (!us_idList.contains(usid1) && ("0".equals(tempLx1)||"".equals(tempLx1)||"null".equals(tempLx1)))//&&us_idList.size()<=100
                    {
                        us_idList.add(usid1);
                        nodeIDList.add(tempCursor.get("SNODID").toString());
                    }
                }
            }
        }
        nodeIDList.clear();
        List list = disposeDataByType(dm,tableNameString,us_idList);
        JSONObject result = new JSONObject();
        result.put("result", list);
        return result;
    }*/
    @Override
    public Map upStreamAnalyze(String dm, String usidParm) {
        List<LINE> list = new ArrayList();
        String tableNameString = tableService.searchLineTable(dm);
        String sql = "select * from \""+tableNameString+"\" where US_ID ='"+usidParm+"'";
        LINE line = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<LINE>(LINE.class));
        getOneUpstreamLine(line,list);
        JSONObject result = new JSONObject();
        result.put("result", list);
        return result;
    }
    public void getOneUpstreamLine(LINE zhswGXLine,List<LINE> list){
        //if(list.size() < 200){
            if("0".equals(zhswGXLine.getLX())){
                StringBuffer sb = new StringBuffer();
                String linetable = tableService.searchLineTable(zhswGXLine.getDM());
                String sql = sb.append("select * from \"").append(linetable)
                        .append("\" where lx = '0' ")
                        .append(" and zddh ='").append(zhswGXLine.getQDDH()).append("'")
                        .toString();
                RowMapper<LINE> rowMapper1  = new BeanPropertyRowMapper<LINE>(LINE.class);
                List<LINE> lineList = jdbcTemplate.query(sql,rowMapper1);
                if(lineList.size() > 0){
                    for(LINE line : lineList){
                        list.add(line);
                        getOneUpstreamLine(line, list);
                    }
                }

                StringBuffer sb1 = new StringBuffer();
                String sql1 = sb1.append("select * from \"").append(linetable)
                        .append("\" where lx = '1' ")
                        .append(" and qddh ='").append(zhswGXLine.getQDDH()).append("'")
                        .toString();
                List<LINE> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
                if(lineList1.size() > 0){
                    for(LINE line : lineList1){
                        list.add(line);
                        getOneUpstreamLine(line, list);
                    }
                }
            }else{
                StringBuffer sb = new StringBuffer();
                String linetable = tableService.searchLineTable(zhswGXLine.getDM());
                String sql = sb.append("select * from \"").append(linetable)
                        .append("\" where lx = '1' ")
                        .append(" and qddh ='").append(zhswGXLine.getZDDH()).append("'")
                        .toString();
                RowMapper<LINE> rowMapper1  = new BeanPropertyRowMapper<LINE>(LINE.class);
                List<LINE> lineList = jdbcTemplate.query(sql,rowMapper1);
                if(lineList.size() > 0){
                    for(LINE line : lineList){
                        list.add(line);
                        getOneUpstreamLine(line, list);
                    }
                }

                StringBuffer sb1 = new StringBuffer();
                String sql1 = sb1.append("select * from \"").append(linetable)
                        .append("\" where lx = '0' ")
                        .append(" and zddh ='").append(zhswGXLine.getZDDH()).append("'")
                        .toString();
                List<LINE> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
                if(lineList1.size() > 0){
                    for(LINE line : lineList1){
                        list.add(line);
                        getOneUpstreamLine(line, list);
                    }
                }
            }
       // }
    }

    //确定起始输入点为起点还是终点
    public String ToQueryID(Map<String, Object> feature)
    {
        String a = "";
        String LX = String.valueOf(feature.get("LX"));
        if (LX.equals("0")||LX.equals("")||LX=="null")//由起点流向终点
        {
            a = feature.get("SNODID").toString();
        }
        else if (LX.equals("1"))
        {
            a = feature.get("ENODID").toString();
        }
        return a;
    }

    //获取满足查询条件 游标
    public List<Map<String, Object>> ToQueryFeatureCursor(String toQueryClause)
    {
        List<Map<String, Object>> list = jdbcTemplate.queryForList(toQueryClause);
        return list;
    }


    /**
     * @Title 根据管线类型处理数据
     * @Description
     * @return
     * @author wangw
     * @date 2020/7/14 0014 11:34
     */
    public List disposeDataByType(String dm,String tableName,List userIdList){
        List list = new ArrayList();
        for (int i = 0; i < userIdList.size(); i++) {
            String sql = "select * from \""+tableName+"\" where US_ID ='"+userIdList.get(i).toString()+"'";
            LINE line = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<LINE>(LINE.class));
            list.add(line);
        }
        return list;
    }
}
